package com.demo;

import com.alibaba.fastjson2.JSON;
import com.mailgun.api.v3.MailgunDomainsApi;
import com.mailgun.api.v3.MailgunMessagesApi;
import com.mailgun.api.v4.MailgunEmailVerificationApi;
import com.mailgun.client.MailgunClient;
import com.mailgun.model.domains.DomainListResponse;
import com.mailgun.model.domains.DomainResponse;
import com.mailgun.model.message.Message;
import com.mailgun.model.message.MessageResponse;
import com.mailgun.util.EmailUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@Slf4j
public class MailGunExample {


  public static void main(String[] args) throws ExecutionException, InterruptedException {
    String apiKey = "3efcf69a840d2522518972c280c0377a-b0ed5083-6b00461d";
    String domain = "email.newhuotech.com";
    String source = "NewHuoTech<mailgun@newhuotech.com>";
//    String toAddresses = "wangfengchen@newhuotech.com";
    String subject = "请查收您的验证码";
    String bodyText = "您的验证码为：123456，5分钟内有效，如不是本人操作，请联系安全部。";
    String bodyHtml = "<html>\n" +
        "<body>\n" +
        "\t<h1>Sending HTML emails with Mailgun</h1>\n" +
        "\t<p style=\"color:blue; font-size:30px;\">Hello world</p>\n" +
        "\t<p style=\"font-size:30px;\">More examples can be found <a href=\"https://documentation.mailgun.com/en/latest/api-sending.html#examples\">here</a></p>\n" +
        "</body>\n" +
        "</html>";

    List<String> toAddresses = new ArrayList<>();
    toAddresses.add("wangfengchen@newhuotech.com");
    toAddresses.add("sunlongfei@newhuotech.com");

    MailgunMessagesApi mailgunMessagesApi = MailgunClient.config(apiKey).createApi(MailgunMessagesApi.class);

    MailgunMessagesApi mailgunAsyncMessagesApi = MailgunClient.config(apiKey).createAsyncApi(MailgunMessagesApi.class);


//    Message message = Message.builder()
//        .from(source)
//        .to(toAddresses)
//        .subject(subject)
//        .text(bodyHtml)
//        .build();

    Message message = Message.builder()
        .from(source)
        .to(toAddresses)
        .subject(subject)
        .text(bodyText)
        .html(bodyHtml)
        .build();


//    MessageResponse messageResponse = mailgunMessagesApi.sendMessage(domain, message);

    CompletableFuture<MessageResponse> result = mailgunAsyncMessagesApi.sendMessageAsync(domain, message);


//    MailgunDomainsApi mailgunDomainsApi = MailgunClient.config(apiKey).createApi(MailgunDomainsApi.class);
//    DomainListResponse domainListResponse = mailgunDomainsApi.getDomainsList();

//    DomainResponse domainResponse = mailgunDomainsApi.verifyDomain(DOMAIN);



    log.info("response = {}", result);
    log.info("response = {}", result.get());
    log.info("response = {}", JSON.toJSONString(result));
  }
}